package org.zuel.lkq.controller;


import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.authz.annotation.RequiresAuthentication;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.zuel.lkq.exception.BizException;
import org.zuel.lkq.param.auth.LoginParam;
import org.zuel.lkq.param.auth.RegisterParam;
import org.zuel.lkq.service.UserService;
import org.zuel.lkq.util.MySecurityUtil;
import org.zuel.lkq.util.ResponseUtil;

@RestController
@Api(tags = "工单系统: 用户授权模块")
@RequestMapping(value="/auth")
public class AuthController {

    private UserService userService;

    @Autowired
    public AuthController(UserService userService) {
        this.userService = userService;
    }

    @ApiOperation(value = "登录")
    @PostMapping(value = "/login2")
    public Object login2(@RequestBody LoginParam user) {
        return userService.login2(user);
    }

    @ApiOperation(value = "注销")
    @GetMapping(value = "/logout")
    public Object logout() {
        return userService.logout();
    }

    @ApiOperation(value = "获取登录用户信息")
    @RequiresAuthentication
    @GetMapping(value = "/showMe")
    public Object showMe() {
        return ResponseUtil.ok(userService.showMe());
    }

    @ApiOperation(value = "注册")
    @PostMapping(value = "/register")
    public Object register(@RequestBody RegisterParam model) {
        try {
            return userService.register(model);
        } catch (BizException e) {
            return ResponseUtil.fail(e.getCode(), e.getMessage());
        }
    }

    @ApiOperation(value = "找出用户的权限")
    @RequiresAuthentication
    @PostMapping(value = "/get_routers")
    public Object getRouters() {
        /*
        if (userId == null) {
            return ResponseUtil.fail(200, "输入的用户id有误！");
        }
        */
        return userService.getRouters(MySecurityUtil.getCurrentUserId());
    }

    /*
    @PostMapping(value = "/login")
    public Object login(@RequestBody LoginParam model) {
        try {
            return userService.login(model);
        } catch (BizException e) {
            return ResponseUtil.fail(e.getCode(), e.getMessage());
        }
    }
    */

}
